package server

import (
	"company_website/src/global"
	"company_website/src/model"
	"company_website/src/model/common"
	"company_website/src/model/response"
)

type Client struct {
}

// GetAllClientInfoPage 分页查找所有用户
func (c *Client) GetAllClientInfoPage(reqInfo *common.GetPageInfo) (*response.GetAllClient, error) {
	resInfo := &response.GetAllClient{}
	var clients []*model.User
	var err error
	db := global.GVL_DB.Model(&model.User{})
	resInfo.PageInfo, err = ServerComment.GetPageRes(reqInfo.Page, db)
	if err != nil {
		return nil, err
	}
	resInfo.PageInfo.PageReqInfo = reqInfo.Page
	db = ServerComment.Paginate(reqInfo.Page, db)
	err = db.Order("id desc").Find(&clients).Error
	if err != nil {
		return nil, err
	}
	var list []*response.ClientMessage
	for _, v := range clients {
		data := &response.ClientMessage{
			Username: v.Username,
			Sex:      v.Sex,
			Phone:    v.Phone,
			Message:  v.Message,
			Status:   v.Status,
			Time:     v.Time,
		}
		list = append(list, data)
	}
	resInfo.ClientInfo = list
	return resInfo, nil
}
